package ;
import lovedna.ds.ByteArray;
import lovedna.events.Event;
import lovedna.game.Actor;
import lovedna.game.CameraClearFlags;
import lovedna.game.Game;
import lovedna.game.IGameMain;
import lovedna.game.Mesh;
import lovedna.game.Shader;
import lovedna.game.ShaderCode;
import lovedna.game.World;
import lovedna.game.components.Camera;
import lovedna.game.components.Light;
import lovedna.game.components.MeshRenderer;
import lovedna.game.components.UIRoot;
import lovedna.game.geometry.GeomSphere;
import lovedna.game.helper.DebugGame;
import lovedna.math.Random;
import lovedna.math.Vector3;
import lovedna.resource.Assets;
import lovedna.resource.data.ResBytes;
import lovedna.ui.TouchManager;
import lovedna.utils.Helper;
import sample1.CubeTexture;
import sample1.Reflection;
import sample1.SkyMap;
import sample1.WaterEff;
import sample1.XRayEff;

/**
 * ... 
 * @author liwei
 */
//@:expose("Client")
class Client implements IGameMain
{
	/*======constants============================*/
	
	/*======static ==============================*/
	public static var instance:Client;
	/*======Public===============================*/
	public var game:Game;
	public var mapWorld:World;
	public var uiroot:UIRoot;
	public var camera:Camera;
	/*======Private==============================*/
	
	public function new() 
	{
		
	}
	/*======Public Static methods================*/
	
	/*======Public methods=======================*/
	public function startGameMain():Void
	{
		instance = this;
		Assets.init("assets");
		#if wx
		//Assets.init("https://lovedna.gitee.io/h5/jump/assets");
		#else
		
		#end
		trace("-------------------");
		game = new Game();
		game.userBackBuffer = false;
		var debug = new DebugGame(game);
		//debug.debugFrame();
		game.color.set(1, 0, 0, 0);
		game.onInit = onGameInit;
		
	}
	/*======public override methods==============*/
	
	/*======private override methods=============*/
	
	/*======Private methods======================*/
	private function onGameInit():Void
	{
		mapWorld = new World();
		mapWorld.name = "world";
		game.addWorld(mapWorld);
		
		
		var cameraParent = new Actor();
		cameraParent.name = "cameraparent";
		mapWorld.addChild(cameraParent);
		
		var actor = new Actor();
		actor.setParent(cameraParent);
		actor.name = "camera";
		camera = actor.addComponent(Camera);
		
		camera.clearFlags.type = CameraClearFlags.All;
		actor.transform.position.set(0, 0, 200).scale(1);
		actor.transform.rotation.lookAt(actor.transform.position, new Vector3());
		
		
		actor = new Actor();
		uiroot = actor.addComponent(UIRoot);
		uiroot.touchInput.enabled = true;
		uiroot.setCamera(camera);
		uiroot.width = 1000;
		uiroot.height = 500;
		mapWorld.addChild(actor);
		
		
		
		
		
		
		
		
		
		
		
		
		
		var version:String = Game.buildVersion + "";
		Assets.setVersion("version.txt", Helper.intToHex(new Random().get()));
		var res:ResBytes = Assets.bytes.get("version.txt");
		res.addEventListener(Event.complete.type, function(?e) {
			var res:ResBytes = Assets.bytes.get("version.txt");
			if (res.enabled)
			{
				var ba:ByteArray = Assets.bytes.get("version.txt").bytes;
				version = ba.toString();
			}
			Assets.version = version;
			Assets.loadMeta("version.data", metaSuccess);
		} );
		
		
	}
	private function metaSuccess():Void
	{
		if (TouchManager.focus)
		{
			onFocus();
			return;
		}
		TouchManager.onBegin.addEventListener(Event.focus.type, onFocus);
		
	}
	private function onFocus(?e):Void
	{
		TouchManager.onBegin.removeEventListener(Event.focus.type, onFocus);
		init();
	}
	private function init():Void
	{
		trace("loading");
		//mapWorld.addChild(new CubeTexture());
		//mapWorld.addChild(new SkyMap());
		//mapWorld.addChild(new Reflection());
		//mapWorld.addChild(new WaterEff());
		mapWorld.addChild(new XRayEff());
		
		
		
		
	}
	/*======get set methods======================*/
	
}